﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    public class DelegateBLL
    {
        //委托方式
        public const string PHONE = "d";
        public const string SMS = "x";
        public const string WEB = "w";


        //委托状态
        public const char UNHANDLED = '0';//未处理
        public const char FAIL = '1'; //取消
        public const char SUCCESS = '9';//成功


        //选举状态
        public enum ElectionState
        {
            Unhandled = 0,  //默认
            Informed = 1,  //已通知
            Completed = 9   //完成
        }

        /// <summary>
        /// 更新委托记录状态
        /// </summary>
        /// <param name="wt">委托记录</param>
        /// <param name="flag">状态</param>
        public void UpdateDelegteFlag(DAL.Wtjl wt, char flag)
        {
            using (DAL.DataClassesDataContext dc = new DAL.DataClassesDataContext())
            {
                DAL.Wtjl newWt = dc.Wtjl.FirstOrDefault(w => w.jlbh == wt.jlbh);
                newWt.flag = flag;
                dc.SubmitChanges();
            }
        }

        /// <summary>
        /// 更新委托人委托状态
        /// </summary>
        /// <param name="wtr">委托人</param>
        /// <param name="flag">状态</param>
        public void UpdateWtrFlag(DAL.Xm wtr, string flag)
        {
            using (DAL.DataClassesDataContext dc = new DAL.DataClassesDataContext())
            {
                DAL.Xm newWtr = dc.Xm.FirstOrDefault(x => x.sfzh == wtr.sfzh);
                newWtr.flag = flag;
                dc.SubmitChanges();
            }
        }

        /// <summary>
        /// 更新被委托人剩余被委托次数 
        /// </summary>
        /// <param name="bwtr">被委托人</param>
        /// <param name="number">次数</param>
        public void UpdateSybwtcs(DAL.Xm bwtr, int number)
        {
            using (DAL.DataClassesDataContext dc = new DAL.DataClassesDataContext())
            {
                DAL.Xm newBwtr = dc.Xm.FirstOrDefault(x => x.sfzh == bwtr.sfzh);
                newBwtr.sybwtcs = number;
                dc.SubmitChanges();
            }
        }

        /// <summary>
        /// 更新选民状态为已回复
        /// </summary>
        /// <param name="xm">选民对象</param>
        public void UpdateReplyFlag(DAL.Xm xm)
        {
            using (DAL.DataClassesDataContext dc = new DAL.DataClassesDataContext())
            {
                DAL.Xm newXm = dc.Xm.FirstOrDefault(x => x.sfzh == xm.sfzh);
                newXm.reply = "Y";
                dc.SubmitChanges();
            }
        }

        /// <summary>
        /// 更新选举为已通知状态
        /// </summary>
        /// <param name="xm">选举对象</param>
        public void UpdateXjFlag(DAL.Xjdj xj)
        {
            using (DAL.DataClassesDataContext dc = new DAL.DataClassesDataContext())
            {
                DAL.Xjdj newXj = dc.Xjdj.FirstOrDefault(x => x.xjbh == xj.xjbh);
                string flag = ((int)BLL.DelegateBLL.ElectionState.Informed).ToString();
                newXj.flag = flag;
                dc.SubmitChanges();
            }
        }

        /// <summary>
        /// 查询未通知过，且距离结束委托时间还有一周的选举
        /// </summary>
        /// <returns></returns>
        public DAL.Xjdj GetXJ()
        {
            using (DAL.DataClassesDataContext dc = new DAL.DataClassesDataContext())
            {
                //查询未通知过，且距离结束委托时间还有一周的选举
                DAL.Xjdj xj = dc.Xjdj.FirstOrDefault(x => x.flag == ((int)ElectionState.Unhandled).ToString() && x.wtjssj.Date == DateTime.Now.Date.AddDays(7));
                return xj;
            }
        }

        /// <summary>
        /// 根据身份证号和选举编号获取选民(被委托人)
        /// </summary>
        /// <param name="sfzh">身份证号</param>
        /// <param name="xjbh">选举编号</param>
        /// <returns></returns>
        public DAL.Xm GetXMBySfzh(string sfzh, string xjbh)
        {
            using (DAL.DataClassesDataContext dc = new DAL.DataClassesDataContext())
            {
                DAL.Xm xm = dc.Xm.FirstOrDefault(x => x.sfzh == sfzh && x.xjbh == xjbh);
                return xm;
            }
        }

        /// <summary>
        /// 根据身份证号(委托人)获取委托记录
        /// </summary>
        /// <param name="wtrsfzh">委托人身份证号</param>
        /// <returns></returns>
        public DAL.Wtjl GetWTJLBySfzh(string wtrsfzh)
        {
            using (DAL.DataClassesDataContext dc = new DAL.DataClassesDataContext())
            {
                DAL.Wtjl wt = dc.Wtjl.FirstOrDefault(w => w.wtrsfzh == wtrsfzh && w.flag == SUCCESS);
                return wt;
            }
        }

        /// <summary>
        /// 根据身份证号获取选民剩余被委托次数
        /// </summary>
        /// <param name="sfzh"></param>
        /// <returns></returns>
        public int GetSybwtcs(string sfzh)
        {
            using (DAL.DataClassesDataContext dc = new DAL.DataClassesDataContext())
            {
                DAL.Xm xm = dc.Xm.First(x => x.sfzh == sfzh);
                if (xm != null)
                {
                    return xm.sybwtcs;
                }
                else
                {
                    return -1;
                }
            }
        }

        /// <summary>
        /// 生成4位委托验证码
        /// </summary>
        /// <returns></returns>
        public string GenerateCode()
        {
            Random rd = new Random();
            string code = rd.Next(9999).ToString();
            for (int i = code.Length; i < 4; i++)
            {
                code = "0" + code;
            }
            return code;
        }

        /// <summary>
        /// 中文汉字检验
        /// </summary>
        /// <param name="strTemp">需判断的字符串</param>
        /// <returns></returns>
        public bool CheckChineseWord(string strTemp)
        {
            foreach (char ch in strTemp)
            {
                //不在汉字范围内 
                if (ch <= 0x4e00 || ch >= 0x9fa5)
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 身份证验证
        /// </summary>
        /// <param name="Id">身份证号</param>
        /// <returns></returns>
        public bool CheckIDCard(string Id)
        {
            if (Id.Length == 18)
            {
                bool check = CheckIDCard18(Id);
                return check;
            }
            else if (Id.Length == 15)
            {
                bool check = CheckIDCard15(Id);
                return check;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 18位身份证验证
        /// </summary>
        /// <param name="Id">身份证号</param>
        /// <returns></returns>
        private bool CheckIDCard18(string Id)
        {
            long n = 0;
            if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false)
            {
                return false;//数字验证
            }
            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (address.IndexOf(Id.Remove(2)) == -1)
            {
                return false;//省份验证
            }
            string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == false)
            {
                return false;//生日验证
            }
            string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
            string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
            char[] Ai = Id.Remove(17).ToCharArray();
            int sum = 0;
            for (int i = 0; i < 17; i++)
            {
                sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
            }
            int y = -1;
            Math.DivRem(sum, 11, out y);
            if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
            {
                return false;//校验码验证
            }
            return true;//符合GB11643-1999标准
        }
        /// <summary>
        /// 15位身份证验证
        /// </summary>
        /// <param name="Id">身份证号</param>
        /// <returns></returns>
        private bool CheckIDCard15(string Id)
        {
            long n = 0;
            if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14))
            {
                return false;//数字验证
            }
            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (address.IndexOf(Id.Remove(2)) == -1)
            {
                return false;//省份验证
            }
            string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-");
            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == false)
            {
                return false;//生日验证
            }
            return true;//符合15位身份证标准
        }

    }
}
